package com.hy.common;

import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Animatable;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @Description :加载时可以动的三个点
 * @Author : wdk
 * @CretaTime : 2020/1/16 12:24
 * @LastModify(最终修改人) :wdk
 * @LastModifyTime(最终修改时间) : 2020/1/16 12:24
 * @LastCheckBy :wdk
 */
public class LoadingPointDrawable extends Drawable implements Animatable {

    private int mProgressDegree = 0;
    private ValueAnimator mValueAnimator;
    private Paint mPaint1 = new Paint();
    private Paint mPaint2 = new Paint();
    private Path mPath = new Path();
    private int radius = 5;
    private int space = 5;
    private int duration = 2000;

    public LoadingPointDrawable() {
        mPaint1.setStyle(Paint.Style.FILL);
        mPaint1.setAntiAlias(true);
        mPaint1.setColor(0xffaaaaaa);

        mPaint2.setStyle(Paint.Style.FILL);
        mPaint2.setAntiAlias(true);
        mPaint2.setColor(0x7faaaaaa);
    }

    /**
     * 设置动画时间
     *
     * @param duration 时间
     * @return this
     */
    public LoadingPointDrawable setDuration(int duration) {
        this.duration = duration;
        setupAnimators();
        return this;
    }

    /**
     * 设置画笔颜色
     *
     * @param oneColor 正常色
     * @param towColor 加了alpha的颜色
     * @return this
     */
    public LoadingPointDrawable setColor(int oneColor, int towColor) {
        mPaint1.setColor(oneColor);
        mPaint2.setColor(towColor);
        return this;
    }

    /**
     * 设置间隔
     *
     * @param space 间隔
     * @return
     */
    public LoadingPointDrawable setSpace(int space) {
        this.space = space;
        return this;
    }

    /**
     * 设置圆的半径
     */
    public LoadingPointDrawable setRadius(int radius) {
        this.radius = radius;
        return this;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.save();
        for (int i = 0; i < 3; i++) {
            mPath.reset();
            mPath.addCircle(radius + i * 2 * radius + i * space, radius, radius, Path.Direction.CW);
            if (mProgressDegree == i) {
                canvas.drawPath(mPath, mPaint1);
            } else {
                canvas.drawPath(mPath, mPaint2);
            }
        }
        canvas.restore();
    }

    @Override
    public void start() {
        if (!mValueAnimator.isRunning()) {
            mValueAnimator.start();
        }
    }

    @Override
    public void stop() {
        if (mValueAnimator.isRunning()) {
            mValueAnimator.cancel();
        }
    }

    @Override
    public boolean isRunning() {
        return mValueAnimator.isRunning();
    }

    @Override
    public void setAlpha(int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    private void setupAnimators() {
        mValueAnimator = ValueAnimator.ofInt(0, 900);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                if (value < 300) {
                    if (mProgressDegree != 0) {
                        mProgressDegree = 0;
                        invalidateSelf();
                    }

                } else if (value < 600) {
                    if (mProgressDegree != 1) {
                        mProgressDegree = 1;
                        invalidateSelf();
                    }

                } else {
                    if (mProgressDegree != 2) {
                        mProgressDegree = 2;
                        invalidateSelf();
                    }
                }
            }
        });
        mValueAnimator.setDuration(duration);
        mValueAnimator.setInterpolator(new LinearInterpolator());
        mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mValueAnimator.setRepeatMode(ValueAnimator.RESTART);
    }
}
